
// 声明专门的函数显示确定模态框
function showConfirmModel(roleArray) {

    $('#modelRoleRemove').modal('show');
    $("#roleNameDiv").empty();

    // 全局roleArrayId
    window.roleIdArray = [];

    for (let i = 0; i < roleArray.length; i++) {
        var role = roleArray[i];
        var roleName = role.roleName;
        $("#roleNameDiv").append(roleName+"<br>");

        var roleId = role.roleId;
        window.roleIdArray.push(roleId);
    }
}


// 执行分页，生成页面效果，任何时候调用这个函数都会重新加载页面
function generatePage() {

    // 1、获取分页数据
    var pageInfo = getPageInfoRemote();

    // 2、填充表格
    fillTableBody(pageInfo);

    $("#summaryBox").prop("checked",false);
}

// 远程访问服务器端程序获取pageInfo数据
function getPageInfoRemote() {
    // 响应数据ajaxResult
    var ajaxResult = $.ajax({
        url: "role/get/page/info.json",
        type: "GET",
        data:
            {
                "pageNum": window.pageNum,
                "pageSize": window.pageSize,
                "keyword": window.keyword
            },
        async: false,
        dataType: "json"
    })


    // 响应码
    var statusCode = ajaxResult.status;

    // 响应是否异常
    if (statusCode != 200){
        layer.msg("失败！响应状态码="+statusCode+"，说明信息："+ajaxResult.statusText);
        return null;
    }

    // 返回的参数
    var resultEntity = ajaxResult.responseJSON;

    // 程序是否异常
    if(resultEntity.result == "FALIED"){
        layer.msg(resultEntity.message);
        return null;
    }

    // 返回pageInfo
    return resultEntity.data;

}

// 填充表格
function fillTableBody(pageInfo) {

    // 清除tbody中的旧内容
    $("#rolePageBody").empty();

    // 清除导航栏
    $("#Pagination").empty();

    if (pageInfo == null || pageInfo == undefined || pageInfo.list == null || pageInfo.list.length == 0){
        var message = "<tr><td colspan='4'>抱歉！暂无数据</td></tr>"
        $("#rolePageBody").append(message);
        return;
    }


    var info = pageInfo.list;


    for (var i = 0; i < info.length; i++) {

        var numTd = "<td>"+(i+1)+"</td>";
        var checkboxTd = "<td><input id='"+info[i].id+"' class='itemBox' type='checkbox'></td>";
        var name = "<td>"+info[i].name+"</td>";

        var checkBtn = "<button id='"+info[i].id+"' type=\"button\" class=\"btn btn-success btn-xs checkBtn\"><i class=\" glyphicon glyphicon-check\"></i></button>";
        var pencilBtn = "<button id='"+info[i].id+"' type=\"button\" class=\"btn btn-primary btn-xs pencilBtn\"><i class=\" glyphicon glyphicon-pencil\"></i></button>";
        var removeBtn = "<button id='"+info[i].id+"' type=\"button\" class=\"btn btn-danger btn-xs removeBtn\"><i class=\" glyphicon glyphicon-remove\"></i></button>";

        var operateTd = "<td>" + checkBtn + " " + pencilBtn + " "+ removeBtn + "</td>";

        var tr = "<tr>"+numTd + checkboxTd + name + operateTd+"</tr>"

        $("#rolePageBody").append(tr);

        generateNavigator(pageInfo);
    }
}

// 生成分页页码导航条
function generateNavigator(pageInfo) {
    var totalRecord = pageInfo.total;

    var properties = {
        num_display_entries: 5, //主体页数
        callback: paginationCallBack,
        items_per_page:pageInfo.pageSize, //每页显示1项
        current_page:pageInfo.pageNum-1,
        prev_text: "上一页",
        next_text: "下一页",
        num_edge_entries: 3 //边缘页数
    }

    $("#Pagination").pagination(totalRecord, properties);
}

// 翻页时的回调函数
function paginationCallBack(pageIndex, jQuery) {
    window.pageNum = pageIndex + 1;
    generatePage()
    // 关闭超链接默认行为
    return false;
}